package com.tedu.bill.demo5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Author：毕龙
 * Date: 2024/5/24 15:14
 */
public class Train2 implements Runnable {
    private static int ticket;//火车票

    //创建锁对象
    Lock lock = new ReentrantLock();

    @Override
    public void run() {//t1  // t2 //t3
        while (true) {
            //获取锁标记
            lock.lock();
            try {
                //当票卖了120张时 退出循环
                if (ticket == 120) {
                    break;
                }
                Thread.sleep(100);
                ticket++;
                System.out.println(Thread.currentThread().getName() + "第" + ticket + "张票卖出");
            } catch (Exception e) {

            } finally {
                //释放锁
                lock.unlock();
            }

        }
    }

}
